package com.atguigu.eduorder.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.Response;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author zhaolie
 * @since 2020-12-20
 */
@RestController
@RequestMapping("/eduorder/order")
@Api(description = "订单前台")
public class OrderController {

    @Resource
    private OrderService orderService;

    @PostMapping("/{courseId}")
    @ApiOperation("生成订单")
    private Response SaveOrder(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        System.out.println(userId);
        String orderNo = orderService.createOrder(courseId, userId);
        return Response.SUCCESS().data("orderId", orderNo);
    }

    @GetMapping("/{orderNo}")
    @ApiOperation("查询订单")
    private Response getOrderInfo(@PathVariable("orderNo") String orderNo) {
        Order order = orderService.getOrderInfo(orderNo);
        return Response.SUCCESS().data("order", order);
    }

    //根据课程id和用户id查询订单表中订单状态
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    @ApiOperation("查询订单是否支付")
    public boolean isBuyCourse(@PathVariable String courseId, @PathVariable String memberId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.eq("member_id", memberId);
        wrapper.eq("status", 1);//支付状态 1代表已经支付
        int count = orderService.count(wrapper);
        if (count > 0) { //已经支付
            return true;
        } else {
            return false;
        }
    }

}

